package cn.zwt.wolf.service.base.language;

import cn.zwt.wolf.domain.base.language.Language;
import cn.zwt.wolf.repository.base.language.LanguageRepository;
import cn.zwt.wolf.util.JPQLSearchException;
import cn.zwt.wolf.util.SearchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.persistence.NoResultException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangwt on 16-3-7.
 */
@Service
public class LanguageService {
    @Autowired
    private LanguageRepository languageRepository;
    @Autowired
    private SearchUtil searchUtil;
    private Logger logger = LoggerFactory.getLogger(LanguageService.class);

    public Language findLanguageById(int id){
        return languageRepository.findOne(id);
    }
    /**
     *
     **/
    public Language findLanguageByName(String name){
        Language language = null;
        if (name == null)
            return language;
        String hql = "from Language where status = 1 and name=:name";
        Map<String,Object> params= new HashMap<String, Object>();
        params.put("name",name);
        try {
            language = (Language) searchUtil.getSingleResult(hql, params);
        } catch (JPQLSearchException e) {
            logger.error("findLanguageByName error :" + e.getMessage());
        }catch (NoResultException e){
            logger.error("findLanguageByName error :" + e.getMessage());
        }
        return language;
    }
    /**
     * 查询信息
     * @param_LanguageDto
     * @return
     */
//    public Page<Language> findLanguageByPage(LanguageDto languageDto){
//        PageRequest pageable = PageUtils.getPageRequest(languageDto);
//        Page<Language> languagePages = null;
//        String hql = "from Language where 1=1 ";
//        Map<String,Object> params = new HashMap<String, Object>();
//        if(languageDto.getName()!=null&&languageDto.getName().length()>0){
//            hql +=" and name like:name";
//            params.put("name", "%" + languageDto.getName() + "%");
//        }
//        if(languageDto.getCode()!=null&&languageDto.getName().length()>0){
//            hql+=" and code like:code";
//            params.put("code", "%" + languageDto.getCode() + "%");
//        }
//        hql +=" and status != -1";
//        try {
//            languagePages = searchUtil.getResultPage(hql, params, pageable);
//        } catch (JPQLSearchException e) {
//            e.printStackTrace();
//        }
//        return languagePages;
//    }
    public Language findLanguageByCode(String code){
        Language language = null;
        if (code == null)
            return language;
        String hql = "from Language where status = 1 and code=:code";
        Map<String,Object> params= new HashMap<String, Object>();
        params.put("code",code);
        try {
            language = (Language) searchUtil.getSingleResult(hql,params);
        } catch (JPQLSearchException e) {
            logger.error("findLanguageByCode error :" + e.getMessage());
        }catch (NoResultException e){
            logger.error("findLanguageByCode error :" + e.getMessage());
        }
        return language;
    }

    public void saveOrUpdate(Language language){
        if (language != null)
            languageRepository.save(language);
    }

//    public void deleteLanguage(int id){
//        Language language = this.findLanguageById(id);
//        if (language != null){
//            language.setStatus(GlobleConstant.STATUS_DELETE_INT);
//        }
//        this.saveOrUpdate(language);
//    }
}

